##What are tilemaps?

It is a map rendered by the browser by individually querying for various vector images and seamlessly joining them.

Different layers of vectors can be overlayed over the map.

##Importing libraries

library(legislatoR)
library(leaflet)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(htmltools)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.4     v stringr 1.4.0
## v tidyr   1.1.3     v forcats 0.5.1
## v readr   2.0.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

##LegislatoR dataset

import the dataset

usaCore <- get_core(legislature = "usa_house")

extract the latitude and longitude values of the birthplace and deathplace into separate columns

usaCore$latBirth <- as.numeric(str_split_fixed(usaCore$birthplace, ",", n = 2)[,1])
usaCore$lngBirth <- as.numeric(str_split_fixed(usaCore$birthplace, ",", n = 2)[,2])

usaCore$latDeath <- as.numeric(str_split_fixed(usaCore$deathplace, ",", n = 2)[,1])
usaCore$lngDeath <- as.numeric(str_split_fixed(usaCore$deathplace, ",", n = 2)[,2])

usaDf <- usaCore |>
  select(1:5, 13:16)

Create the tilemap, and add markers for each data point. lat and lng are required to position the marker; color sets different colours for the birth and death places; label allows us to display the name of the corresponding legislator when the user hovers the cursor over the marker; popup allows us to display the other variables when a marker is clicked; and group allows us to group the markers into separate layers so we can choose what to display using addLayersControl(), making the map interactive.

leaflet(data = usaCore) |>
  addTiles() |>
  addCircleMarkers( lng = ~lngBirth,
                    lat = ~latBirth,
                    color = "red",
                    radius = 0.2,
                    label = ~name,
                    group = "Born in",
                    popup = ~paste0( "Name: ", name, "<br>",
                                     "Sex: ", sex, "<br>",
                                     "Ethnicty: ", ethnicity, "<br>",
                                     "Religion: ", religion, "<br>")
                    
  ) |>
  addCircleMarkers( lng = ~lngDeath,
                    lat = ~latDeath,
                    color = "blue",
                    radius = 0.2,
                    label = ~name,
                    group = "Died in",
                    popup = ~paste0( "Name: ", name, "<br>",
                                     "Sex: ", sex, "<br>",
                                     "Ethnicty: ", ethnicity, "<br>",
                                     "Religion: ", religion, "<br>")
  ) |>
  addLayersControl(overlayGroups = c("Born in", "Died in"),
                   options = layersControlOptions(collapsed = FALSE))
## Warning in validateCoords(lng, lat, funcName): Data contains 1364 rows with
## either missing or invalid lat/lon values and will be ignored
## Warning in validateCoords(lng, lat, funcName): Data contains 3091 rows with
## either missing or invalid lat/lon values and will be ignored

##sieges dataset

In the second dataset, we display a map of some of the most important sieges in military history. We create a colour palette mapping a shade of colour to an year over the domain of the period of time encompassing the data points. We then set the colour of the marker to the one generated by the palette for the corresponding year. The size of the markers are variable, and is used to indicate the number of casualties in each siege. Labels give the war during which the siege took place.

df1 <- read.csv("sieges.csv")
#A <- df1$Conflict
#A <- match(A, unique(A))
pal <- colorNumeric("plasma", domain = df1$Year)
leaflet(data = df1) |>
  addTiles() |>
  addCircleMarkers( lng = ~lon,
                    lat = ~lat,
                    color = ~pal(Year),
                    radius = ~(Casualties/100000),
                    label = ~as.character(Conflict)
  )

##WWII dataset

In the third dataset, we display the targets of all the allied bombing raids during world war 2.We can create a pallette to use colour to indicate which nation carried out the attack, or how much ordinance was dropped, and set the other as a label.

df2 <- read.csv("THOR_WWII_DATA_CLEAN.csv")
A <- df2$COUNTRY_FLYING_MISSION
A <- match(A, unique(A))
pal <- colorNumeric("inferno", domain = A)
pal2 <- colorNumeric("viridis", domain = df2$TOTAL_TONS)
leaflet(data = df2) |>
  addTiles() |>
  addCircleMarkers( lng = ~LONGITUDE,
                    lat = ~LATITUDE,
                    color = ~pal(A),
                    label = as.character(paste(df2$TOTAL_TONS), " tons of explosives"),
                    #clusterOptions = markerClusterOptions(),
                    weight = 1)
## Warning in validateCoords(lng, lat, funcName): Data contains 8738 rows with
## either missing or invalid lat/lon values and will be ignored

Since the number of markers is very large, we can use clustering to improve readability and load time. Clustering dynamically groups nearby markers based on the zoom level.

leaflet(data = df2) |>
  addTiles() |>
  addCircleMarkers( lng = ~LONGITUDE,
                    lat = ~LATITUDE,
                    color = ~pal(A),
                    label = as.character(paste(df2$TOTAL_TONS), " tons of explosives"),
                    clusterOptions = markerClusterOptions(),
                    weight = 1)
## Warning in validateCoords(lng, lat, funcName): Data contains 8738 rows with
## either missing or invalid lat/lon values and will be ignored